home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_01 / ed_157 / load_post.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  6.5 KB  |  254 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record
  3.  * Copyright (C) 1993 by Charles Sandmann (sandmann@clio.rice.edu)
  4.  * 
  5.  * This file is part of ED.
  6.  * 
  7.  * ED is free software; you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation.
  9.  * 
  10.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  11.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  13.  * 
  14.  * You should have received a copy of the GNU General Public License along with ED
  15.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  16.  * Mass Ave, Cambridge, MA 02139, USA.
  17.  */
  18. #include "opsys.h"
  19.  
  20. #ifndef NO_NEWS
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <time.h>
  26.  
  27. #include "unistd.h"
  28. #include "memory.h"
  29. #include "rec.h"
  30. #include "window.h"
  31. #include "ed_dec.h"    /* global data */
  32.  
  33. extern Char *get_servername();
  34. extern Char *version();
  35.  
  36. /******************************************************************************\
  37. |Routine: count_equals
  38. |Callby: load_post
  39. |Purpose: Returns a count of how many '='s precede a subject entry.
  40. |Arguments:
  41. |    rec is the record being examined.
  42. \******************************************************************************/
  43. Int count_equals(rec)
  44. rec_ptr rec;
  45. {
  46.     Char *p,*q;
  47.     
  48.     p = q = rec->data + 42;
  49.     while(*q++ == '=');
  50.     return(--q - p);
  51. }
  52.  
  53. /******************************************************************************\
  54. |Routine: get_post_info
  55. |Callby: load_post
  56. |Purpose: Returns the username and host.
  57. |Arguments:
  58. |    user is the returned user name.
  59. |    host is the returned host name.
  60. |    hostsize is the size of host.
  61. \******************************************************************************/
  62. void get_post_info(user,host,hostsize)
  63. Char *user,*host;
  64. Int hostsize;
  65. {
  66.     Char *p;
  67.     
  68. #ifdef CUSERID_ENV
  69.     strcpy(user,getenv(CUSERID_ENV));
  70. #else
  71.     strcpy(user,cuserid(NULL));
  72. #endif
  73.     gethostname(host,hostsize);
  74.     if(!strchr(host,'.'))
  75.     {
  76. #ifdef UCX
  77.         if((p = getenv("UCX$BIND_DOMAIN")))
  78.         {
  79.             strcat(host,".");
  80.             strcat(host,p);
  81.         }
  82. #else
  83. #ifdef WOLLONGONG
  84.         if((p = getenv("INET_DOMAIN_NAME")))
  85.         {
  86.             strcat(host,".");
  87.             strcat(host,p);
  88.         }
  89. #else
  90. /*        getdomainname(domain,domainsize); */
  91. #endif
  92. #endif
  93.     }
  94. }
  95.     
  96. /******************************************************************************\
  97. |Routine: load_post_string
  98. |Callby: load_post
  99. |Purpose: Creates a new records, loads a string, appends to a buffer.
  100. |Arguments:
  101. |    base is the base of the buffer that receives the new record.
  102. |    string is the string to append.
  103. \******************************************************************************/
  104. void load_post_string(base,string)
  105. rec_ptr base;
  106. Char *string;
  107. {
  108.     rec_ptr new;
  109.     
  110.     new = (rec_ptr)imalloc(sizeof(rec_node));
  111.     new->data = (Char *)imalloc((new->length = strlen(string)) + 1);
  112.     strcpy(new->data,string);
  113.     new->recflags = 1;
  114.     insq(new,base->prev);
  115. }
  116.     
  117. /******************************************************************************\
  118. |Routine: load_post
  119. |Callby: edit
  120. |Purpose: Loads the headers of a news posting.
  121. |Arguments:
  122. |    base is the base of the buffer that receives the headers.
  123. |    subject is the subject of the posting.
  124. \******************************************************************************/
  125. void load_post(base,subject)
  126. rec_ptr base;
  127. Char *subject;
  128. {
  129.     Char buf[512],subjbuf[512],resp[512],user[128],host[128],email[256],name[256];
  130.     Char *p,*q,*msg,*tmp,*m,*new;
  131.     rec_ptr r;
  132.     FILE *fp;
  133.     time_t t;
  134. #ifdef VMS
  135.     Char wdy[8],mon[8];
  136.     Int day,hour,min,sec,year;
  137. #endif
  138.     Int artnum,i;
  139.  
  140. /* get the identifying information */
  141.     get_post_info(user,host,sizeof(host));
  142. /* try to get the real name from the passwd file */
  143.     name[0] = '\0';
  144.     if((fp = fopen("/etc/passwd","r")))
  145.     {
  146.         while(fgets(buf,sizeof(buf),fp))
  147.             if((p = strchr(buf,':')))
  148.             {
  149.                 *p = '\0';
  150.                 if(!strcmp(user,buf))
  151.                     if((p = strchr(++p,':')))
  152.                         if((p = strchr(++p,':')))
  153.                             if((p = strchr(++p,':')))
  154.                                 if((q = strchr(++p,':')))
  155.                                 {
  156.                                     *q = '\0';
  157.                                     strcpy(name,p);
  158.                                     break;
  159.                                 }
  160.             }
  161.         fclose(fp);
  162.     }
  163.     sprintf(email,"%s@%s",user,host);
  164.     sprintf(buf,"Relay-Version: version ED-%s; site %s",version(),host);
  165.     load_post_string(base,buf);
  166.     sprintf(buf,"Posting-Version: version ED-%s; site %s",version(),host);
  167.     load_post_string(base,buf);
  168.     if(name[0] != '\0')
  169.         sprintf(buf,"From: %s <%s>",name,email);
  170.     else
  171.         sprintf(buf,"From: <%s>",email);
  172.     load_post_string(base,buf);
  173.     t = time(0);
  174. #ifdef VMS
  175. /*Sun Sep 16 01:03:52 1973\n\0*/
  176.     my_sscanf(ctime(&t),"%s%s%d%d:%d:%d%d",wdy,mon,&day,&hour,&min,&sec,&year);
  177.     sprintf(buf,"Date: %s, %2d %s %4d %2d:%2d:%2d",wdy,day,mon,year,hour,min,sec);
  178. #else
  179.     strftime(buf,sizeof(buf),"Date: %a, %d %b %Y %T %Z",localtime(&t));
  180. #endif
  181.     load_post_string(base,buf);
  182.     sprintf(buf,"Newsgroups: %s",WINDOW[CURWINDOW].filename);
  183.     load_post_string(base,buf);
  184.     sprintf(buf,"Message-ID: <%08x.%s>",time(NULL),email);
  185.     load_post_string(base,buf);
  186.     sprintf(buf,"Path: %s!%s",get_servername(),user);
  187.     load_post_string(base,buf);
  188. /* handle posting references */
  189.     if(CURREC != BASE)
  190.     {
  191.         if(strncmp(subject,"Re: ",4) && strncmp(subject,"re: ",4))
  192.         {
  193.             sprintf(subjbuf,"Re: %s",subject);
  194.             subject = subjbuf;
  195.         }
  196.         r = CURREC;
  197.         msg = NULL;
  198.         while(1)
  199.         {
  200.             my_sscanf(r->data,"%d",&artnum);
  201.             sprintf(buf,"STAT %d",artnum);
  202.             news_command(buf);
  203.             news_response(resp,sizeof(resp));
  204.             if(resp[0] == '2')
  205.             {
  206.                 news_command("HEAD");
  207.                 news_response(resp,sizeof(resp));
  208.                 if(resp[0] == '2')
  209.                     while(1)
  210.                     {
  211.                         news_response(resp,sizeof(resp));
  212.                         if(!strcmp(resp,"."))
  213.                             break;
  214.                         if(!strncmp(resp,"Message-ID: ",strlen("Message-ID: ")))
  215.                             if((m = strchr(resp,'<')))
  216.                                 if(!msg)
  217.                                 {
  218.                                     msg = (Char *)imalloc(strlen(m) + 1);
  219.                                     strcpy(msg,m);
  220.                                 }
  221.                                 else
  222.                                 {
  223.                                     new = (Char *)imalloc(strlen(msg) + 1 + strlen(m) + 1);
  224.                                     sprintf(new,"%s %s",m,msg);
  225.                                     ifree(msg);
  226.                                     msg = new;
  227.                                 }
  228.                     }
  229.             }
  230. /* new back up to record with one less '=' */
  231.             if(!(i = count_equals(r)))
  232.                 break;
  233.             do
  234.             {
  235.                 if((r = r->prev) == BASE)
  236.                     break;
  237.             } while(count_equals(r) != i - 1);
  238.         }
  239.         tmp = (Char *)imalloc(strlen("References: ") + strlen(msg) + 1);
  240.         sprintf(tmp,"References: %s",msg);
  241.         load_post_string(base,tmp);
  242.         ifree(msg);
  243.         ifree(tmp);
  244.     }
  245.     sprintf(buf,"Subject: %s",subject);
  246.     load_post_string(base,buf);
  247.     sprintf(buf,"Reply-To: %s",email);
  248.     load_post_string(base,buf);
  249.     buf[0] = '\0';
  250.     load_post_string(base,buf);
  251. }
  252. #endif
  253.  
  254.